package com.choudou5.solr.service;

import cn.hutool.core.date.DateUtil;
import com.choudou5.solr.annotation.ServiceInfo;
import com.choudou5.solr.framework.bean.UserBean;
import com.choudou5.solr.framework.bean.UserSessionBean;
import com.choudou5.solr.framework.constants.SysConsts;
import com.choudou5.solr.framework.constants.UserConsts;
import com.choudou5.solr.framework.util.UserUtil;
import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.SysSeqUtil;
import com.choudou5.solr.util.jdbc.DBUtil;
import com.choudou5.solr.util.jdbc.SqlFormatUtil;
import com.choudou5.solr.utils.PasswordUtil;
import com.choudou5.solr.utils.UserUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @name 系统用户 接口
 * @author choudou5
 * @date 2018-7-13
 */
@ServiceInfo(table = "solr_sys_user", desc = "系统用户")
public class SysUserService extends BaseService<UserBean> {

    public List<UserBean> getSelectListAll() {
        String sql = "SELECT id, name FROM "+getTable();
       return findListBySql(sql);
    }

    public UserBean getByPhone(Long phone) {
        String sql = SqlFormatUtil.format("SELECT * FROM ? WHERE phone = ?", getTable(), phone);
        try {
            return DBUtil.findOne(sql, UserBean.class);
        } catch (SQLException e) {
            logger.error("getByPhone fail", e);
        }
        return null;
    }

    public void save(UserBean bo) {
        if(bo.isNew()){
            bo.setId(genId());
            bo.setCreateTime(new Date());
            add(bo);
        }else{
            bo.setUpdateTime(new Date());
            update(bo);
        }
    }

    private Integer genId(){
        return DBUtil.getMaxId(getTable(), 999) + 1;
    }

    public UserBean registerByOauth(String userName, String userIp) {
        UserBean bo = new UserBean();
        Integer userId = genId();
        bo.setId(userId);
        bo.setName(StrUtil.setDefVal(userName, bo.getId() + "用户"));
        bo.setPassword(PasswordUtil.entrypt(userId, SysSeqUtil.generateCode()));
        bo.setRole(UserConsts.UserRole.NORMAL.getKey());
        bo.setStatus(UserConsts.UserStatus.NORMAL.getKey());
        bo.setLoginIp(userIp);
        Date now = new Date();
        bo.setLoginTime(now);
        bo.setCreateTime(now);
        bo.setCreateBy(1);
        add(bo);
        return bo;
    }

    /**
     * 更新状态
     * @param userId
     * @param userStatus
     * @param reason
     */
    public void updateStatus(Integer userId, UserConsts.UserStatus userStatus, String reason) {
        UserBean bo = new UserBean();
        bo.setId(userId);
        bo.setStatus(userStatus.getKey());
        bo.setRemarks(reason);
        bo.setUpdateBy(UserUtil.getUserId());
        bo.setUpdateTime(new Date());
        updateByNonNull(bo);
        addSynDynamic(SysConsts.ActionType.UPDATE, "修改"+ UserUtils.getUserName(userId)+" 状态为："+userStatus.getDesc(), reason);
    }


    public void loginSucc(UserBean user, String ip) {
        user.setLoginIp(ip);
        user.setLoginTime(new Date());
        String sql = SqlFormatUtil.format("UPDATE ? SET login_ip = ?, login_time = ?  WHERE id = ?", getTable(), ip, DateUtil.now(), user.getId());
        update(sql);

        addSynDynamic(SysConsts.ActionType.LOGIN, "登录了系统", user);
    }

    public void loginOut(UserSessionBean user) {
        if(user == null)
            return;
        addSynDynamic(SysConsts.ActionType.LOGIN_OUT, "退出了系统", user);
        UserUtil.removeUserSession();
    }

}
